package vip.mtdp.customer.service.rpc.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import vip.mtdp.common.CommonConstant;
import vip.mtdp.common.exception.AppRTException;
import vip.mtdp.customer.dal.dao.PartyMapper;
import vip.mtdp.customer.dal.dao.PersonMapper;
import vip.mtdp.customer.dal.domain.Party;
import vip.mtdp.customer.dal.domain.Person;
import vip.mtdp.customer.api.dto.PartyDTO;
import vip.mtdp.customer.api.dto.PersonDTO;
import vip.mtdp.customer.api.PersonRpcService;

import java.time.LocalDateTime;

/**
 * 
 *
 * @Description 个人客户服务
 * @author wangguoqing
 * @date 2017年9月17日上午10:19:35
 *
 */
@Slf4j
@Service
public class PersonRpcServiceImpl implements PersonRpcService {

	@Autowired
	private PartyMapper partyMapper;

	@Autowired
	private PersonMapper personMapper;


	/**
	 * 新增客户
	 *
	 * @param personDTO
	 * @return
	 */
	@Transactional(rollbackFor = RuntimeException.class)
	@Override
	public PersonDTO createPerson(PersonDTO personDTO) {
		log.info("personDTO = {}",personDTO);
		if(personDTO == null || personDTO.getPartyDTO() == null){
			throw new AppRTException("param_is_not_null","personDTO is not null");
		}
		Person person = new Person();
		BeanUtils.copyProperties(personDTO,person);
		Party party = new Party();
		PartyDTO partyDTO = personDTO.getPartyDTO();
		BeanUtils.copyProperties(partyDTO,party);
		//生成用户编号
		String no = String.valueOf(System.nanoTime());
		party.setNo(no);
		LocalDateTime d = LocalDateTime.now();
		party.setCreatedAt(d);
		party.setUpdatedAt(d);
		//默认启用
		party.setState(CommonConstant.ENALBED);
		partyMapper.save(party);
		Long partyId = party.getOid();
		partyDTO.setId(partyId);

		person.setPartyId(partyId);
		person.setCreatedAt(d);
		person.setUpdatedAt(d);
		personMapper.save(person);
		personDTO.setId(person.getOid());
		personDTO.getPartyDTO().setId(partyId);
		return personDTO;
	}

	/**
	 * 更新客户
	 *
	 * @param personDTO
	 * @return
	 */
	@Transactional(rollbackFor = RuntimeException.class)
	@Override
	public boolean updatePerson(PersonDTO personDTO) throws AppRTException{
		log.info("personDTO = {}",personDTO);
		if(personDTO == null){
			throw new AppRTException("param_is_not_null","personDTO is not null");
		}
		//先更新person,再更新party
		Person person = new Person();
		BeanUtils.copyProperties(personDTO,person);
		person.setOid(personDTO.getId());
		LocalDateTime d = LocalDateTime.now();
		person.setUpdatedAt(d);
		int cnt = personMapper.update(person);
		if(cnt != 1){
			log.error("update person fail");
			throw new AppRTException("update_person_fail","update person fail");
		}
		PartyDTO partyDTO = personDTO.getPartyDTO();
		if(partyDTO != null){
			Party party = new Party();
			BeanUtils.copyProperties(partyDTO,party);
			party.setUpdatedAt(d);
			cnt = partyMapper.update(party);
			if(cnt != 1){
				log.error("update party fail");
				throw new AppRTException("update_party_fail","update party fail");
			}
		}
		return true;
	}

	/**
	 * 根据partyId 查询客户信息
	 *
	 * @param partyId
	 * @return
	 */
	@Override
	public PersonDTO getPersonByPartyId(Long partyId) throws AppRTException {
		log.info("partyId = {}",partyId);
		Party party = partyMapper.get(partyId);
		if(party == null){
			throw new AppRTException("query_party_is_null","query party is null");
		}
		Person person = personMapper.queryPersonByPartyId(partyId);
		PartyDTO partyDTO = new PartyDTO();
		BeanUtils.copyProperties(party,partyDTO);
		PersonDTO personDTO = new PersonDTO();
		BeanUtils.copyProperties(person,personDTO);
		personDTO.setPartyDTO(partyDTO);
		return personDTO;
	}

}